package com.example.asus.shixun2.model;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;

import java.util.Random;

/**
 * Created by ASUS on 2019/1/1.
 */

public class BoxsModel {

    public  Point[] boxs;//方块
    public final int TYPE = 7;//方块种类
    public int boxtype;//方块类型
    public int boxSize;//方块大小
    Paint box_paint;//声明方块画笔
    public Point[] boxsNext;//下一块方块
    public int boxtypeNext;//下一块方块类型
    public int boxNextSize;//下一块方块格子大小

    public BoxsModel(int boxSize){
        this.boxSize = boxSize;
//        initpaint();

    }

    /**新的方块*/
   public void newBoxs() {
       if(boxsNext == null){//方块为空，执行生成下一块方法
           newBoxsNext();
       }
       //当前方块 = 下一方块
       boxs = boxsNext;
       boxtype = boxtypeNext;
        //随机生成方块种类序号
        Random r = new Random();
       boxtypeNext = r.nextInt(7);
       initpaint(boxtypeNext);
       newBoxsNext();
        //初始化方块
       switch(boxtypeNext){
           case 0 :
               boxsNext = new Point[]{new Point(6,0),new Point(7,0),new Point(6,1),new Point(7,1)};
               break;
           case 1 :
               boxsNext = new Point[]{new Point(6,1),new Point(7,0),new Point(5,1),new Point(7,1)};
               break;
           case 2 :
               boxsNext = new Point[]{new Point(6,1),new Point(5,0),new Point(5,1),new Point(7,1)};
               break;
           case 3 :
               boxsNext = new Point[]{new Point(7,0),new Point(6,0),new Point(8,0),new Point(9,0)};
               break;
           case 4 :
               boxsNext = new Point[]{new Point(7,1),new Point(7,0),new Point(6,1),new Point(8,1)};
               break;
           case 5 :
               boxsNext = new Point[]{new Point(7,1),new Point(6,0),new Point(6,1),new Point(8,1)};
               break;
           case 6 :
               boxsNext = new Point[]{new Point(7,1),new Point(7,0),new Point(6,1),new Point(6,2)};
               break;
       }
    }
    /**生成下一块*/
    public void newBoxsNext(){
        Random r = new Random();
        boxtypeNext = r.nextInt(7);
        initpaint(boxtypeNext);
        //初始化方块
        switch(boxtypeNext){
            case 0 :
                boxsNext = new Point[]{new Point(6,0),new Point(7,0),new Point(6,1),new Point(7,1)};
                break;
            case 1 :
                boxsNext = new Point[]{new Point(6,1),new Point(7,0),new Point(5,1),new Point(7,1)};
                break;
            case 2 :
                boxsNext = new Point[]{new Point(6,1),new Point(5,0),new Point(5,1),new Point(7,1)};
                break;
            case 3 :
                boxsNext = new Point[]{new Point(7,0),new Point(6,0),new Point(8,0),new Point(9,0)};
                break;
            case 4 :
                boxsNext = new Point[]{new Point(7,1),new Point(7,0),new Point(6,1),new Point(8,1)};
                break;
            case 5 :
                boxsNext = new Point[]{new Point(7,1),new Point(6,0),new Point(6,1),new Point(8,1)};
                break;
            case 6 :
                boxsNext = new Point[]{new Point(7,1),new Point(7,0),new Point(6,1),new Point(6,2)};
                break;
        }
    }

    /**绘制方块*/
    public void drawBoxs(Canvas canvas){
        //方块绘制
        if(boxs != null)//判断方块不为空
            for(int i = 0;i < boxs.length;i ++){
                canvas.drawRect(boxs[i].x*boxSize,boxs[i].y*boxSize,boxs[i].x*boxSize+boxSize,boxs[i].y*boxSize+boxSize,box_paint);
            }
    }

    /**初始化方块画笔*/
    private Paint initpaint(int boxtype) {
        switch (boxtype){
            case 0 :
                box_paint = new Paint();//初始化方块画笔
                box_paint.setColor(Color.rgb(102,0,205));
                break;
            case 1 :
                box_paint = new Paint();//初始化方块画笔
                box_paint.setColor(Color.rgb(0,0,250));
                break;
            case 2 :
                box_paint = new Paint();//初始化方块画笔
                box_paint.setColor(Color.rgb(0,122,1));
                break;
            case 3 :
                box_paint = new Paint();//初始化方块画笔
                box_paint.setColor(Color.rgb(255,255,1));
                break;
            case 4 :
                box_paint = new Paint();//初始化方块画笔
                box_paint.setColor(Color.rgb(254,0,2));
                break;
            case 5 :
                box_paint = new Paint();//初始化方块画笔
                box_paint.setColor(Color.rgb(255,246,5));
                break;
            case 6 :
                box_paint = new Paint();//初始化方块画笔
                box_paint.setColor(Color.rgb(253,0,255));
                break;
        }
        box_paint.setAntiAlias(true);//打开抗锯齿
        return box_paint;
    }

    /**移动*/
    public boolean move(int x,int y,MapModel mapModel){
        Log.e("移动前",boxs[0].x+":"+boxs[0].y);
        //遍历方块数组，每一个都加上偏移的量
        for(int i = 0;i < boxs.length;i ++){
            //将方块将要移动的位置传入判断方块在移动后是否会出界
            if(checkBoundary(boxs[i].x + x,boxs[i].y + y,mapModel)){
                return false;
            }
        }
        for(int i = 0;i < boxs.length;i ++){
            //将方块将要移动的位置传入判断方块在移动后是否会出界
            boxs[i].x += x;
            boxs[i].y += y;
        }
        Log.e("移动后",boxs[0].x+":"+boxs[0].y);
        return true;
    }


    /**旋转*/
    public boolean rotate(MapModel mapModel){
        if(boxtype == 0){//方块为田字型，旋转失败
            return false;
        }
        //遍历方块数组，每一个都绕着中心点顺时针旋转90度
        for(int i = 0;i < boxs.length;i ++){
            //旋转算法（笛卡尔公式），顺时针旋转90度
            int checkX = -boxs[i].y + boxs[0].y +boxs[0].x;
            int checkY = boxs[i].x - boxs[0].x + boxs[0].y;
            //将旋转后的位置传入边界判断是否出界
            if(checkBoundary(checkX,checkY,mapModel)){
                //如果出界false，旋转失败
                return  false;
            }
        }
        //遍历方块数组，每一个都绕着中心点顺时针旋转90度
        for(int i = 0;i < boxs.length;i ++){
            //旋转算法（笛卡尔公式），顺时针旋转90度
            int checkX = -boxs[i].y + boxs[0].y +boxs[0].x;
            int checkY = boxs[i].x - boxs[0].x + boxs[0].y;
            boxs[i].x = checkX;
            boxs[i].y = checkY;
        }
        return true;
    }


    /**边界判断*/
    /**
     * 出界判断
     * 传入x,y
     * @param x 方块x坐标
     * @param y 方块y坐标
     * @return true 出界 false 未出界
     */
    public boolean checkBoundary(int x,int y ,MapModel mapModel){
        return(x<0 || y<0 || x >= mapModel.maps.length || y >= mapModel.maps[0].length || mapModel.maps[x][y] == true);
    }

    /**绘制下一块预览*/
    public void drawNext(Canvas canvas) {
        if(boxsNext != null){
            if(boxNextSize == 0){
                boxNextSize = 24;
            }
            for(int i = 0;i < boxsNext.length;i ++){
                canvas.drawRect((boxsNext[i].x-5)*boxNextSize,(boxsNext[i].y+2)*boxNextSize,(boxsNext[i].x-5)*boxNextSize+boxNextSize,(boxsNext[i].y+2)*boxNextSize+boxNextSize,box_paint);
            }
        }
    }
}
